package com.feixiu.mapreduce.wordcount1;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**\
 * <KEYIN,reduce 阶段输入的key的类型    ：Text
 * VALUEIN, reduce阶段  输入的value的类型：LongWritable
 * KEYOUT,  reduce阶段输出的key的类型：Text
 * VALUEOUT>   reduce阶段输出的value的类型：IntWritable
 * reduce阶段输入的key和value应该和  map阶段输出的东西一样
 *
 */
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    /**
     * 每个key调用一次，是会有多个值的
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        for (IntWritable value:values) {
            sum+=value.get();
        }
        IntWritable outV = new IntWritable();
        outV.set(sum);
        context.write(key,outV);
    }
}
